import { reactive, inject } from "vue";
import { useProjectStore } from '../../../store/useProjectStore'

export const useProjectSettingTable = (currentPage: any, pageSize: any) => {
    //获取API配置
    const API = inject("API") as any;
    const ApiConfig = inject("ApiConfig") as any;
    const projectSettingTable = reactive(
        {
            total: 0,
            tableData: [],
            queryCondition: {
                svalue: '',
            },
            settingTypes:[
                {
                    value:'状态',
                    label:'状态',
                }
            ],
        }
    );

    const projectStore = useProjectStore();

    //查询
    const queryData = async (stype) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Project.GetAllProjectSetting,
                {
                    pageIndex: currentPage.value,
                    pageSize: pageSize.value,
                    condition: { svalue: projectSettingTable.queryCondition.svalue, stype: stype },
                }
            );
            if (response.status == "200" && response.data.code == "200") {
                projectSettingTable.tableData = response.data.data;
                projectSettingTable.total = response.data.count;
            }
        } catch (error) {
            console.error(error);
        }
    };

    //新增
    const addData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Project.CreateProjectSetting,
                projectStore.projectSettingModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //更新
    const updateData = async () => {
        try {
            const response = await API.request(
                ApiConfig.Methods.POST,
                ApiConfig.PATH.Project.UpdateProjectSetting,
                projectStore.projectSettingModel
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }
    //删除
    const deleteData = async (projectSettingId) => {
        try {
            const response = await API.request(
                ApiConfig.Methods.GET,
                ApiConfig.PATH.Project.DeleteByProjectSettingId,
                { projectSettingId: projectSettingId }
            );
            if (response.status == "200" && response.data.code == "200") {
                return true;
            }
        } catch (error) {
            console.error(error);
        }
        return false;
    }


    return { projectSettingTable, projectStore, queryData, addData, updateData, deleteData };
};